#include "frmdeviceconfig.h"
#include "ui_frmdeviceconfig.h"
#include <QTcpSocket>
#include <QSerialPort>

frmdeviceconfig::frmdeviceconfig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::frmdeviceconfig),
    mainWindow(qobject_cast<frmmain*>(parent)),
    isCommandMode(false)
{
    ui->setupUi(this);

    connect(mainWindow, &frmmain::serialConnectionChanged, this, &frmdeviceconfig::onSerialConnectionChanged);
    connect(mainWindow, &frmmain::tcpConnectionChanged, this, &frmdeviceconfig::onTcpConnectionChanged);
}

frmdeviceconfig::~frmdeviceconfig()
{
    delete ui;
}

void frmdeviceconfig::on_enterCommandModeButton_clicked()
{
    if (!mainWindow->getIsTcpConnected() && !mainWindow->getIsSerialConnected()) {
        updateMainResponse("错误：请先建立连接");
        return;
    }

    sendCommandModeCommand(true);
    isCommandMode = true;
    ui->enterCommandModeButton->setEnabled(false);
    ui->exitCommandModeButton->setEnabled(true);
    enableConfigControls(true);
    updateMainResponse("已进入指令模式");
}

void frmdeviceconfig::on_exitCommandModeButton_clicked()
{
    if (!mainWindow->getIsTcpConnected() && !mainWindow->getIsSerialConnected()) {
        updateMainResponse("错误：请先建立连接");
        return;
    }

    sendCommandModeCommand(false);
    isCommandMode = false;
    ui->enterCommandModeButton->setEnabled(true);
    ui->exitCommandModeButton->setEnabled(false);
    enableConfigControls(false);
    updateMainResponse("已退出指令模式");
}

void frmdeviceconfig::on_sendConfigButton_clicked()
{
    if (!isCommandMode) {
        updateMainResponse("错误：请先进入指令模式");
        return;
    }

    QString param = ui->paramLineEdit->text().trimmed();
    if (param.isEmpty()) {
        updateMainResponse("错误：参数不能为空");
        return;
    }

    QString command = QString("$PARAM,%1\r\n").arg(param);
    if (mainWindow->getIsTcpConnected()) {
        QTcpSocket *tcpSocket = mainWindow->getTcpSocket();
        tcpSocket->write(command.toUtf8());
        tcpSocket->flush();
    } else if (mainWindow->getIsSerialConnected()) {
        QSerialPort *serialPort = mainWindow->getSerialPort();
        serialPort->write(command.toUtf8());
        serialPort->flush();
    }

    updateMainResponse("已发送配置: " + command.trimmed());
}

void frmdeviceconfig::on_cancelButton_clicked()
{
    resetConfigFields();
    enableConfigControls(false);
    if (isCommandMode) {
        sendCommandModeCommand(false);
        isCommandMode = false;
        ui->enterCommandModeButton->setEnabled(true);
        ui->exitCommandModeButton->setEnabled(false);
    }
    updateMainResponse("已取消操作");
}

void frmdeviceconfig::onSerialConnectionChanged(bool connected)
{
    ui->enterCommandModeButton->setEnabled(connected && !isCommandMode);
    ui->exitCommandModeButton->setEnabled(connected && isCommandMode);
}

void frmdeviceconfig::onTcpConnectionChanged(bool connected)
{
    ui->enterCommandModeButton->setEnabled(connected && !isCommandMode);
    ui->exitCommandModeButton->setEnabled(connected && isCommandMode);
}

void frmdeviceconfig::sendCommandModeCommand(bool enter)
{
    QString command = enter ? "$CFG+\r\n" : "$CFG-\r\n";
    if (mainWindow->getIsTcpConnected()) {
        QTcpSocket *tcpSocket = mainWindow->getTcpSocket();
        tcpSocket->write(command.toUtf8());
        tcpSocket->flush();
    } else if (mainWindow->getIsSerialConnected()) {
        QSerialPort *serialPort = mainWindow->getSerialPort();
        serialPort->write(command.toUtf8());
        serialPort->flush();
    }
}

void frmdeviceconfig::updateMainResponse(const QString &msg)
{
    if (mainWindow) {
        QTextEdit *responseTextEdit = mainWindow->findChild<QTextEdit*>("responseTextEdit");
        if (responseTextEdit) {
            responseTextEdit->append(msg);
        }
    }
}

void frmdeviceconfig::enableConfigControls(bool enable)
{
    ui->paramLineEdit->setEnabled(enable);
    ui->sendConfigButton->setEnabled(enable);
    ui->cancelButton->setEnabled(enable);
}

void frmdeviceconfig::resetConfigFields()
{
    ui->paramLineEdit->clear();
}